package com.jfinal.bbsapp.my;
import java.util.List;
import org.apache.log4j.Logger;

import com.jfinal.bbsapp.common.kit.CommonUtils;
import com.jfinal.bbsapp.common.kit.MyAnnotation;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

@MyAnnotation(memo="我的业务类")
public class MyService {
	private final Logger logger = Logger.getLogger(MyService.class);
	
	@MyAnnotation(memo="我的课程")
	public Ret courselist(String token) {
		try {
			String userid=CommonUtils.getuseridbytoken(token);
			StringBuilder sql = new StringBuilder();
			sql.append("select cast(a.courseid as char) courseid,a.coursename,a.img,a.classhour,a.memo,a.sortno");
			sql.append(" from c_course a join c_card_sure b on a.courseid=b.courseid where b.cuserid= ?");
			sql.append(" order by sortno desc");
			List<Record> list = Db.find(sql.toString(),userid);
			return Ret.ok("data", list).set("code", 20000);
		} catch (Exception e) {
			logger.error("------Exception------", e);
			return Ret.fail("系统异常").set("code", 50000);
		}
	}
	

	@MyAnnotation(memo="我的学习记录")
	public Ret studylist(String token) {
		try {
			String userid=CommonUtils.getuseridbytoken(token);
			StringBuilder sql = new StringBuilder();
			sql.append("select cast(a.recordid as char) recordid,cast(a.chapterid as char) chapterid,");
			sql.append("b.coursename,b.img,b.memo,b.chapternum,a.chaptername,a.watchrate,from_unixtime(a.ctime/1000) ctime");
			sql.append(" from c_user_record a join c_course b on a.courseid=b.courseid where a.cuserid= ?");
			sql.append(" order by ctime desc");
			List<Record> list = Db.find(sql.toString(),userid);
			return Ret.ok("data", list).set("code", 20000);
		} catch (Exception e) {
			logger.error("------Exception------", e);
			return Ret.fail("系统异常").set("code", 50000);
		}
	}
}